package com.caxxb.controller;

import com.caxxb.common.QR;
import com.caxxb.common.R;
import com.caxxb.entity.Game;
import com.caxxb.service.GameService;
import com.caxxb.service.impl.GameServiceImpl;
import com.caxxb.util.PageData;
import com.caxxb.util.QiniuUtils;
import com.caxxb.util.QueryResponseData;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.util.*;

@RestController
@RequestMapping("game")
public class GameController {


    private GameService gameService=new GameServiceImpl();
    private QR<Game> qr = new QR<>();
    private QueryResponseData queryResponseData = new QueryResponseData();


    //热销游戏
    @GetMapping("/fire")
    public R<List>  getCarouselImg(){
        List imgs = gameService.getFireImg();
        return R.success(imgs);
    }

    //最新游戏
    @GetMapping("/new")
    public R<List>  getNewImg(){
        List imgs = gameService.getNewlImg();
        return R.success(imgs);
    }

    //游戏分类分页的方法
    @GetMapping("page")
    public R page(@RequestParam(value = "keywords",defaultValue ="",required = false )String keywords, @RequestParam(value = "pageNo",defaultValue ="1",required = false )int pageNo, @RequestParam(value = "pageSize",defaultValue ="10",required = false )int pageSize, HttpSession session){
        int gametypeid=1;
        if(session.getAttribute("typeid")!=null){
            gametypeid = (int) session.getAttribute("typeid");
        }

        System.out.println("typeid:"+gametypeid);
        PageData<Game> gamePages = gameService.getGamePages(gametypeid,keywords, pageNo, pageSize);
        return R.success(gamePages);
    }

    //查询
    @GetMapping("query")
    public QueryResponseData getGame(@RequestParam(value = "keywords",defaultValue ="",required = false )String keywords, @RequestParam(value = "page",defaultValue ="1",required = false )int pageNo, @RequestParam(value = "limit",defaultValue ="10",required = false )int pageSize){
        PageData<Game> game = gameService.getGame(keywords, pageNo, pageSize);
        queryResponseData.setCount(game.getTotal());
        queryResponseData.setData(game.getData());
        return queryResponseData;
    }

    //id查询
    @PutMapping("update/{game_id}")
    public R<Boolean> getGame(@PathVariable int game_id,@RequestBody Game game){
        boolean flag = gameService.updateGame(game_id, game);
        return R.success(flag);
    }


    /**
     * 下架游戏
     * @param game_id
     * @return
     */
    @PutMapping("del/{game_id}")
    public R<Boolean> delGame(@PathVariable int game_id){
        boolean flag = gameService.delGame(game_id);
        if (flag == false){
            return R.error("下架失败");
        }else{
            return R.success(flag);
        }
    }


    //图
    @GetMapping("echart")
    public R getData(){
        Map<String,Object> data=new HashMap<>();
        List<Map<String,Object>> gameValues=new ArrayList<>();
        List<Game> games = gameService.getGames();

        for (Game game : games) {
            Map map = new HashMap<>();
            map.put("value",game.getSales());
            map.put("name",game.getGame_name());
            gameValues.add(map);
        }
        data.put("gameValues",gameValues);
        System.out.println(gameValues);
        return R.success(data);

    }

    //重新生成的图片名
    String imgname=null;

    @PostMapping("upload")
    public R fileUpload(MultipartFile game_img) throws IOException {

        System.out.println("game_img:"+game_img);
        String originalFilename = game_img.getOriginalFilename();
        //获取文件后缀
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        //给文件重新生成名字
        imgname  = UUID.randomUUID().toString()+suffix;
//        System.out.println("imgname:"+imgname);

        //文件路径
        String filepath="D://中软//project//dreamgame//src//main//webapp//images//"+imgname;

        File img=new File(filepath);
        System.out.println("文件存储路径:"+filepath);

//        将图片上传到七牛云
        QiniuUtils.upload2Qiniu(game_img.getBytes(),imgname);
//        将图片复制到本地
        game_img.transferTo(img);

        return R.success(imgname);
    }

    //新增游戏的方法
    @PostMapping("add")
    public R addGame(String game_name, Double price, String game_developer, @RequestParam(value = "release_time",required = false,defaultValue = "")String release_time,  Integer typeid, HttpServletRequest request) throws IOException {


//        System.out.println("name:"+game_name);

//        System.out.println("price:"+price);

//        System.out.println("deve:"+game_developer);
        if(release_time.equals("")){
            return R.error("请输入时间");
        }

        LocalDate time = LocalDate.parse(release_time);
//        System.out.println("time:"+time);

        System.out.println("img:"+imgname);

        boolean flag = gameService.addGame(game_name, price, game_developer, time, imgname, typeid);
        if(flag){
            return R.success(flag);
        }else {
            return R.error("添加失败");
        }

    }

}
